package com.my.lucky.service;


import com.my.lucky.model.StockMainCost;
import com.my.lucky.repository.StockMainCostRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service
public class StockMainCostService {

    @Autowired
    private StockMainCostRepository stockMainCostRepository;

    /**
     * 将股票主力成本数据保存到数据库
     */
    public void saveStockMainCostToDB(Map<String, Double> stockData, LocalDate searchDate) {
        if (stockData == null || stockData.isEmpty()) {
            log.info("没有数据需要保存到数据库");
            return;
        }

        try {
            // 可以选择先删除该日期的旧数据，再插入新数据
            stockMainCostRepository.deleteBySearchDate(searchDate);
            log.info("已删除日期为 {} 的旧数据", searchDate);

            // 转换为实体列表
            List<StockMainCost> entities = stockData.entrySet().stream()
                    .map(entry -> new StockMainCost(entry.getKey(), entry.getValue(), searchDate))
                    .collect(Collectors.toList());

            // 批量保存
            List<StockMainCost> savedEntities = stockMainCostRepository.saveAll(entities);
            log.info("成功保存 {} 条股票主力成本数据到数据库", savedEntities.size());
        } catch (Exception e) {
            log.error("保存股票主力成本数据到数据库失败", e);
            throw new RuntimeException("保存股票数据失败", e);
        }
    }

    /**
     * 根据日期查询并转换为Map<String, Double>
     */
    public Map<String, Double> getStockMainCostByDate(LocalDate searchDate) {
        if (searchDate == null) {
            log.warn("查询日期不能为空");
            return new HashMap<>(0);
        }

        try {
            stockMainCostRepository.deleteBySearchDateBefore(searchDate.minusDays(25));
            List<StockMainCost> stockList = stockMainCostRepository.findBySearchDate(searchDate);
            if (stockList == null || stockList.isEmpty()) {
                log.info("日期 {} 无对应数据", searchDate);
                return new HashMap<>(0);
            }

            // 转换为Map<String, Double>，过滤空值
            return stockList.stream()
                    .filter(stock -> stock.getMainCost() != null)
                    .collect(Collectors.toMap(
                            StockMainCost::getStockCode,
                            StockMainCost::getMainCost,
                            (existing, replacement) -> existing  // 处理重复代码
                    ));

        } catch (Exception e) {
            log.error("查询股票主力成本数据失败", e);
            return new HashMap<>(0);
        }
    }

    // 其他辅助方法...
    private String formatWithSimpleDateFormat(String keyword) {
        // 实现方法逻辑
        return keyword;
    }

    private Double parseNumbers(String value) {
        // 实现方法逻辑
        try {
            return Double.parseDouble(value);
        } catch (NumberFormatException e) {
            return null;
        }
    }
}
